// app/controller/users.js
const Controller = require('./baseController')
const { Op } = require('sequelize')

class PermissionController extends Controller {
	async list() {
		const { ctx } = this

		let options = {
			where: {
				rid: [1, 2],
			},
		}
		const res = await ctx.service.permission.list(options)
		let mid = []
		let prim = []
		res.forEach((e) => {
			mid.push(e.dataValues.mid)
			prim.push(e.dataValues.prim)
		})
		mid = [...new Set(mid)]
		prim = [...new Set(prim)]
		// console.log(mid, prim)
		// let arr = []
		// rows.forEach((element) => {
		// 	arr.push(element.dataValues)
		// })
		// arr = ctx.helper.Tree2(arr)

		// console.log(res)

		ctx.helper.success({
			data: {
				mid,
				prim,
			},
		})
	}
	async getRemark() {
		const { ctx } = this
		if (!ctx.query.id) {
			ctx.helper.fail({ msg: '关键字缺少' })
			return
		}
		let id = ctx.query.id
		const data = await ctx.model.RoleMenu.findAll({
			attributes: ['prim'],
			where: {
				rid: {
					[Op.eq]: id,
				},
			},
		})
		let arr = data.map((item) => item.dataValues.prim)
		ctx.helper.success({ data: arr })
	}
	async updated() {
		const { ctx } = this
		// 参数校验
		const { rid, midPrims } = ctx.request.body
		if (!rid) {
			ctx.helper.fail({ msg: '参数缺失' })
			return
		}
		if (!midPrims) {
			ctx.helper.fail({ msg: '参数缺失' })
			return
		}
		const res = await ctx.service.permission.update(rid, midPrims)
		if (res.flag) {
			ctx.helper.success({ msg: res.msg })
		}
	}
}
module.exports = PermissionController
